package com.dm.yyxr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dm.yyxr.mapper.UserMapper;
import com.dm.yyxr.pojo.Area;
import com.dm.yyxr.pojo.Community;
import com.dm.yyxr.pojo.User;
import com.dm.yyxr.pojo.dto.UserDto;
import com.dm.yyxr.service.AreaService;
import com.dm.yyxr.service.CommunityService;
import com.dm.yyxr.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    AreaService areaService;
    @Autowired
    CommunityService communityService;

    /**
     * 根据用户名获取用户信息
     * @param user_name
     * @return
     */
    @Override
    public User getByUserName(String user_name) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUser_name,user_name);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     *接收用户ID获取用户Dto
     * @param id
     * @return
     */
    @Override
    public UserDto getUserDtoById(Integer id) {
        User user = baseMapper.selectById(id);
        if(user==null){
            return null;
        }
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user,userDto);
        Area area = areaService.getById(user.getArea_code());
        Community community = communityService.getById(user.getCommunity_code());
        userDto.setArea(area.getArea_name());
        userDto.setCommunity(community.getCommunity_name());
        return userDto;
    }
}
